package com.fourth.app.func;

import com.fourth.utils.KeywordUtil;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.FunctionHint;
import org.apache.flink.table.functions.TableFunction;
import org.apache.flink.types.Row;

import java.io.IOException;
import java.util.List;

/**
 * @author wjy
 * @create 2022-08-20 9:33
 */
@FunctionHint(output = @DataTypeHint("ROW<word STRING>"))
public class splitFunction extends TableFunction<Row> {

    public void eval(String keyword) {

        List<String> list;
        try {
            //调用ik分词器，将返回结果赋值给list集合
            list = KeywordUtil.splitKeyword(keyword);
            //遍历集合，将拆分后的单词写出 实现了一进多出
            for (String word : list) {
                collect(Row.of(word));
            }
        } catch (IOException e) {
            //切词失败，抓取异常，将原词写出
            collect(Row.of(keyword));
        }
    }
}
